//
//  SEGHouseUserListViewController.m
//  ExcellentHome
//
//  Created by Chiang on 16/8/9.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGHouseUserListViewController.h"
#import "SEGUserManagerCell.h"
#import "SEGNewAlertView.h"

@interface SEGHouseUserListViewController ()<UITableViewDelegate,UITableViewDataSource, SEGNewAlertViewDelegate>{
    NSInteger row;
}
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,assign) BOOL canDel;
@property(nonatomic, strong) SEGHouseUserListViewModel *viewModel;
@end

@implementation SEGHouseUserListViewController
#pragma mark - life cycle
- (instancetype)initWithViewModel:(SEGHouseUserListViewModel*)viewModel{
    if (self == [super init]) {
        _viewModel = viewModel;
    }
    return self;
}

-(void)loadView
{
    [super loadView];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), CGRectGetHeight(self.view.bounds) - kNavigationBarAndStatusBarHeight) style:UITableViewStylePlain];
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
    self.tableView.backgroundColor=[UIColor clearColor];
    self.tableView.separatorColor=kColor_L2;
    self.tableView.tableFooterView=[[UIView alloc] init];
    [self.tableView registerClass:[SEGUserManagerCell class] forCellReuseIdentifier:kSEGUserManagerCellIdentifier];
    [self.view addSubview:self.tableView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"成员管理";
    
    [self showCustomNavigationBackButton];
    
    
    [self segInitViews];
    [self bindViewModel];
}

#pragma mark - UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 63;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SEGUserManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:kSEGUserManagerCellIdentifier forIndexPath:indexPath];
    if (self.viewModel.dataArray.count > indexPath.row) {
        SEGUserInfoModel *model = self.viewModel.dataArray[indexPath.row];
        [cell setUpUserManagerCellWithModel:model];
        // 判断用户身份权限，如果当前用户是业主,显示“删除”按钮
        BOOL canDelete = NO;
        if([self.viewModel.userType isEqualToString:@"2"]) {//当前用户是业主
            if ([model.houseRelation isEqualToString:@"2000"] || [model.houseRelation isEqualToString:@"2001"]||[model.houseRelation isEqualToString:@"1001"]) {
                canDelete = YES;
            }
        }
        cell.deleteButton.hidden = !canDelete;
        cell.deleteButton.tag = indexPath.row;
        
        if (cell.deleteButton.allTargets.count == 0) {
            [cell.deleteButton addTarget:self action:@selector(seg_deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (self.viewModel.houseName && self.viewModel.houseName.length > 0) {
        CGSize baseSize = CGSizeMake(LYScreenWidth() - 30, CGFLOAT_MAX);
        CGSize labelsize  = [self.viewModel.houseName
                            boundingRectWithSize:baseSize
                            options:NSStringDrawingUsesLineFragmentOrigin
                            attributes:@{NSFontAttributeName:kFont_16}
                            context:nil].size;
        return labelsize.height+10>44?labelsize.height+10:44;
    }else {
        return 0.01;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.viewModel.houseName && self.viewModel.houseName.length > 0) {
        CGSize baseSize = CGSizeMake(LYScreenWidth() - 30, CGFLOAT_MAX);
        CGSize labelsize  = [self.viewModel.houseName
                            boundingRectWithSize:baseSize
                            options:NSStringDrawingUsesLineFragmentOrigin
                            attributes:@{NSFontAttributeName:kFont_16}
                            context:nil].size;
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), labelsize.height+10>44?labelsize.height+10:44)];
        view.backgroundColor = kColor_bg;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, CGRectGetWidth(view.frame)-30, labelsize.height>34?labelsize.height:34)];
        label.font = kFont_16;
        label.numberOfLines = 0;
        label.textColor = kColor_1;
        [view addSubview:label];
        label.text = self.viewModel.houseName;
        return view;
    }else {
        return [UIView new];
    }
    
}

#pragma 删除房屋
-(void)seg_deleteButtonPressed:(UIButton*)sender
{
    row = sender.tag;
    SEGNewAlertView *alertView = [[SEGNewAlertView alloc] init];
    alertView.delegate = self;
    [alertView showWithAlertTitle:@"是否删除该房屋成员" text:@"删除后，需要重新认证才能使用物业功能" delegate:self buttonTitlesArray:@[@"取消", @"确认删除"]];
}

#pragma mark - SEGNewAlertViewDelegate
- (void)segNewAlertView:(UIView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self.viewModel.delHouseUserDataCommand execute:@(row)];
        [SVProgressHUD show];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Init Views
-(void)segInitViews{}

#pragma mark - Layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
}

#pragma mark - Private Methods

#pragma mark - RAC Data Binding
- (void)bindViewModel {
    @weakify(self);
    
    [[[self.viewModel.requestDataCommand executionSignals] switchToLatest] subscribeNext:^(id  _Nullable x) {
        
        @strongify(self)
        
        [SVProgressHUD dismiss]; //解除加载遮罩层
        
        [self.tableView reloadData];
    }];
    
    [[[self.viewModel.delHouseUserDataCommand executionSignals]switchToLatest] subscribeNext:^(id  _Nullable x) {
        
        @strongify(self)
        
        [SVProgressHUD dismiss]; //解除加载遮罩层
        
        //根据房屋ID,查询房屋下的用户列表
        [self.viewModel.requestDataCommand execute:nil];
        [SVProgressHUD show];
    }];
    
    //根据房屋ID,查询房屋下的用户列表
    [self.viewModel.requestDataCommand execute:nil];
    [SVProgressHUD show];
}

#pragma mark - getter
-(SEGHouseUserListViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[SEGHouseUserListViewModel alloc]init];
    }
    return _viewModel;
}
#pragma mark setter
@end

